﻿// ------------------------------------------------------------------------------
// <copyright file="RelayCommand.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.  All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLib
{
    using System;
    using System.Windows.Input;

    /// <summary>
    /// Command to be used to implement MVVM pattern
    /// </summary>
    public class RelayCommand : ICommand
    {
        /// <summary>
        /// Variable to hold reference to method in view model that is to be executed when this command is executed
        /// </summary>
        private readonly Action<object> execute;

        /// <summary>
        /// Variable to hold reference to validation method in view model
        /// </summary>
        private readonly Predicate<object> canExecute;

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class"
        /// </summary>
        /// <param name="execute">Reference to execute method in view model which will be called when this command is fired</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class"
        /// </summary>
        /// <param name="execute">Reference to execute method in view model which will be called when this command is fired</param>
        /// <param name="canExecute">Reference to an optional validation method in view model to be executed before the command method is executed</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            this.execute = execute;
            this.canExecute = canExecute;
        }

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should execute 
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// ICommand.CanExecute. Determines whether the command can execute in its current state
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null</param>
        /// <returns>True if this command can be executed; otherwise, false.</returns>
        public bool CanExecute(object parameter)
        {
            return this.canExecute == null ? true : this.canExecute(parameter);
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null</param>
        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }
}
